<!--
	作者：1048107945@qq.com
	时间：2020-02-29
	描述：月薪管理主页面
	handleCreated 制作工资-确定
	handleEditShow 修改某一条记录

	goSalaryMake 去往工资制作页面
	goSalaryIssue 去往工资补发页面
-->
<template>
  <div class="app-container clearfix">
    <!-- 搜索 -->
    <salary-search
      :init-params="initParams"
      :search-params="searchKey"
      :department-all-list-data="departmentAllListData"
      @on-init="handleInitList"
    />
    <!-- 列表 -->
    <salary-table
      :data="listData"
      :search-params="searchKey"
      :total-count="totalCount"
      :list-loading="listLoading"
      @on-init="handleInitList"
      @on-edit="handleEditShow"
      @on-back="handleBackShow"
      @on-view="handleViewShow"
      @on-delete="handleDelete"
    />
    <!-- 编辑弹出框 -->
    <salary-make
      :visible.sync="editInfo.visible"
      :department-all-list-data="departmentAllListData"
      @on-created="handleCreated"
    />
    <!-- 撤回审批弹出框 -->
    <salary-back
      :visible.sync="backInfo.visible"
      :detail-data="backInfo.detailInfo"
      @on-init="handleInitList"
    />
  </div>
</template>

<script>
import { getSalaryListAPI, deleteSalaryAPI } from '@/api/todo/salary'
import { SalarySearch, SalaryTable, SalaryMake, SalaryBack } from './components'
import treeAllListData from '@/mixins/treeAllListData'

export default {
  name: 'SalaryList',
  components: {
    SalarySearch,
    SalaryTable,
    SalaryMake,
    SalaryBack
  },
  mixins: [treeAllListData],
  props: {},
  data() {
    return {
      initParams: { // 初始化参数
        pageNum: 1, // 当前页数
        pageSize: 10, // 每页个数
        // sort: '', // 排序
        deptId: null, // 部门ID
        salaryMonth: '' // 发放月份
      },
      searchKey: {}, // 搜索参数
      editInfo: { // 编辑弹出框
        visible: false, // 是否显示模态框
        detailInfo: {}
      },
      backInfo: { // 撤回弹出框
        visible: false, // 是否显示模态框
        detailInfo: {}
      },
      totalCount: 0, // 分页用的列表总条数
      listLoading: false,
      listData: [] // 列表数据
    }
  },
  computed: {},
  watch: {
    initParams: {
      handler: function(newVal, oldVal) {
        console.log(JSON.stringify(newVal))
        if (JSON.stringify(newVal) !== JSON.stringify(this.searchKey)) {
          this.searchKey = JSON.parse(JSON.stringify(newVal))
        }
      },
      deep: true,
      immediate: true
    }
  },
  created() {
  },
  mounted() {
    this.initData()
  },
  methods: {
    goSalaryreissue({ type, data }) { // 进入工资补发
      let query = { type }
      if (type === '1') {
        query = {
          ...query,
          ...data
        }
      } else {
        query['id'] = data.id
      }
      // console.log(query)
      this.$router.push({
        name: 'Salaryreissue',
        query
      })
    },
    goSalaryMake({ type, data }) { // 进入月薪制作
      let query = { type }
      if (type === '1') {
        query = {
          ...query,
          ...data
        }
      } else {
        query['id'] = data.id
      }
      // console.log(query)
      this.$router.push({
        name: 'Salarymake',
        query
      })
    },
    goStockSalaryMake({ type, data }) { // 进入股份公司月薪制作
      let query = { type }
      if (type === '1') {
        query = {
          ...query,
          ...data
        }
      } else {
        query['id'] = data.id
        query['title'] = data.title
      }
      this.$router.push({
        name: 'stockSalarymake',
        query
      })
    },
    handleEditShow(data = {}) { // 编辑－显示弹出框
      if (data) {
        // 跳工资补发页面
        if (data.salaryType == 20) {
          this.goSalaryreissue({
            type: '2',
            data
          })
          return
        }
        // 跳股份工资
        if (data.salaryType == 11) {
          this.goStockSalaryMake({
            type: '2',
            data
          })
          return
        }

        this.goSalaryMake({
          type: '2',
          data
        })
      } else {
        this.editInfo.visible = true
      }
    },
    handleBackShow(data = {}) { // 撤回审批－显示弹出框
      this.backInfo.visible = true
      this.backInfo.detailInfo = data
    },
    handleViewShow(data = {}) {
      if (data) {
        this.goSalaryMake({
          type: '3',
          data
        })
      }
    },
    handleCreated(data = {}) { // 添加
      console.log('页面跳转之前的参数为：', data)
      // 跳工资补发页面
      if (data.salaryType === 20) {
        this.goSalaryreissue({
          type: '1',
          data
        })
        return
      }
      // 跳股份工资
      if (data.salaryType === 11) {
        this.goStockSalaryMake({
          type: '1',
          data
        })
        return
      }
      // 跳月薪制作页面
      this.goSalaryMake({
        type: '1',
        data
      })
    },
    handleUpdated() { // 编辑成功
      this.getListData()
    },
    handleDelete(data = {}) { // 删除
      this.$confirm('确定要删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteSalaryData(data.id)
      }).catch(() => {
      })
    },
    handleInitList(params = {}) { // 初始化列表
      this.searchKey = {
        ...this.searchKey,
        ...params
      }
      this.getListData()
    },
    async deleteSalaryData(id) { // 添加－请求数据
      try {
        const response = await deleteSalaryAPI({
          id
        })
        const result = response
        if (+result.code === 200) {
          this.handleInitList()
          this.$notify({
            title: '成功',
            message: '删除成功！',
            type: 'success',
            duration: 2000
          })
        } else {
          this.$notify.error({
            title: '错误',
            message: result.msg || '删除失败！'
          })
        }
      } catch (error) {
        // console.log(error)
        this.$notify.error({
          title: '错误',
          message: error.message || '删除失败！'
        })
      }
    },
    async getDepartmentAllListData() { // 获取所有数据
      try {
        await this.$store.dispatch('department/GetDepartmentList', {
          request: true,
          showChargedOnly: true
        })
      } catch (error) {
        // console.log(error)
        this.$notify.error({
          title: '错误',
          message: error.message
        })
      }
    },
    async getListData() { // 获取列表数据
      try {
        const { searchKey } = this
        this.listLoading = true
        const response = await getSalaryListAPI({
          ...searchKey
        })
        const result = response
        if (+result.code === 200) {
          this.listData = result.rows || []
          this.totalCount = result.total || 0
        } else {
          this.$notify.error({
            title: '错误',
            message: result.msg || '获取列表失败！'
          })
        }
      } catch (error) {
        // console.log(error)
        this.$notify.error({
          title: '错误',
          message: error.message || '获取列表失败!'
        })
      } finally {
        this.listLoading = false
      }
    },
    async initData() { // 初始化数据
      this.getListData()
      this.getDepartmentAllListData()
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
